package feeinterface.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ulwx.tool.DbUtils;
import com.ulwx.tool.StringUtils;

import feeinterface.modeldb.ChannelCloseProvincePhonePrex;

/**
 * 通道省份屏蔽号段表
 * 
 * @author 黄杰
 * 
 */
public class ChannelCloseProvincePhonePrexDao {
	private static Logger log = Logger
			.getLogger(ChannelCloseProvincePhonePrexDao.class);

	/**
	 * 获取此号码被屏蔽的通道
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<Integer> getCloseInfo(Integer provinceID, String phone)
			throws Exception {
		List<Integer> channelList = new ArrayList<Integer>();
		String sql = "select * from ChannelCloseProvincePhonePrex a where (a.ProvinceID=? or "
				+ "a.ProvinceID=0) " + " ";
		try {
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(1, provinceID);
			List<ChannelCloseProvincePhonePrex> list = DbUtils.doQueryClass(
					null, ChannelCloseProvincePhonePrex.class, sql, map);
			if (list != null && list.size() > 0) {
				for (ChannelCloseProvincePhonePrex prex : list) {
					if (prex.getPhonePrex() != null
							&& StringUtils.hasText(prex.getPhonePrex())) {
						String[] phones = StringUtils.split(
								prex.getPhonePrex(), ",");
						for (String str : phones) {
							// 如果手机号码是以此开头
							if (StringUtils.hasText(str)) {
								str=StringUtils.trim(str);
								if (phone.startsWith(str)) {
									channelList.add(prex.getChannelID());
									break;
								}
							}
						}
					}
				}
				log.info("被屏蔽的号段的通道:" + channelList.toString());
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return channelList;
	}
}
